import { createRouter, createWebHistory } from 'vue-router'
import AdminLogin from '@/views/login/AdminLogin.vue'
// admin
import HomePage from '@/views/admin/HomePage.vue'
import Main from '@/views/admin/Main.vue'
import UserManager from '@/views/admin/role_manager/UserManager.vue'
import TypeManager from '@/views/admin/keyword_manager/TypeManager.vue'
import TagManager from '@/views/admin/keyword_manager/TagManager.vue'
import ModelManager from '@/views/admin/keyword_manager/ModelManager.vue'
import ContentExamine from '@/views/admin/examine/ContentExamine.vue'
import ReviewExamine from '@/views/admin/examine/ReviewExamine.vue'
import LimitManager from '@/views/admin/LimitManager.vue'
import SensitivityManager from '@/views/admin/SensitivityManager.vue'
import AdminManager from '@/views/admin/role_manager/AdminManager.vue'
import ArgumentManager from '@/views/admin/sys_manager/ArgumentManager.vue'
import NoticeManager from '@/views/admin/sys_manager/NoticeManager.vue'

import UserLogin from '@/views/login/UserLogin.vue'
import Home from '@/views/client/Home.vue'
import UMain from '@/views/client/UMain.vue'
import ContentPush from '@/views/client/ContentPush.vue'
import UserPage from '@/views/client/UserPage.vue'
import Community from '@/views/client/Community.vue'
import ContentManager from '@/views/client/ContentManager.vue'
import PublishedCommunity from '@/views/client/pulishCommunity/PublishedCommunity.vue'
import CommunityDetail from '@/views/client/pulishCommunity/CommunityDetail.vue'
import Rank from '@/views/client/rank/Rank.vue'
import Other from '../views/client/other/Other.vue'

import ErrorPage from '@/views/error/404.vue'
import BlogDetail from '@/views/client/BlogDetail.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/admin',
      name: 'HomePage',
      component: HomePage,
      redirect: '/main',
      meta: { isLogin: true, grade: 'ADMIN' },
      children: [
        {
          path: '/main',
          name: 'Main',
          component: Main,
          meta: { isLogin: true, grade: 'ADMIN' }
        },
        {
          path: '/usermanager',
          name: 'UserManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: UserManager
        },
        {
          path: '/content',
          name: 'ContentExamine',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: ContentExamine
        },
        {
          path: '/review',
          name: 'ReviewExamine',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: ReviewExamine
        },
        {
          path: '/type',
          name: 'TypeManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: TypeManager
        },
        {
          path: '/tag',
          name: 'TagManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: TagManager
        },
        {
          path: '/model',
          name: 'ModelManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: ModelManager
        },
        {
          path: '/role',
          name: 'LimitManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: LimitManager
        },
        {
          path: '/sensitivity',
          name: 'SensitivityManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: SensitivityManager
        },
        {
          path: '/adminmanager',
          name: 'AdminManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: AdminManager
        },
        {
          path: '/argument',
          name: 'ArgumentManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: ArgumentManager
        },
        {
          path: '/notice',
          name: 'NoticeManager',
          meta: { isLogin: true, grade: 'ADMIN' },
          component: NoticeManager
        }
      ]
    },
    {
      path: '/',
      name: 'Home',
      component: Home,
      redirect: '/umain',
      meta: { isLogin: true, grade: 'CLIENT' },
      children: [
        {
          path: '/umain',
          name: 'UMain',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: UMain
        },
        {
          path: '/userinfo:userId',
          name: 'UserPage',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: UserPage
        },
        {
          path: '/community',
          name: 'Community',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: Community
        },
        {
          path: '/communitydetail:id',
          name: 'CommunityDetail',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: CommunityDetail
        },
        {
          path: '/rank',
          name: 'Rank',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: Rank
        },
        {
          path: '/other',
          name: 'Other',
          meta: { isLogin: true, grade: 'CLIENT' },
          component: Other
        }
      ]
    },
    {
      path: '/alogin',
      name: 'AdminLogin',
      component: AdminLogin
    },
    {
      path: '/ulogin',
      name: 'UserLogin',
      component: UserLogin
    },
    {
      path: '/error',
      name: 'ErrorPage',
      component: ErrorPage
    },
    {
      path: '/publish',
      name: 'ContentPush',
      meta: { isLogin: true, grade: 'CLIENT' },
      component: ContentPush
    },
    {
      path: '/blogdetail',
      name: 'BlogDetail',
      meta: { isLogin: true, grade: 'CLIENT' },
      component: BlogDetail
    },
    {
      path: '/contentManager',
      name: 'ContentManager',
      meta: { isLogin: true, grade: 'CLIENT' },
      component: ContentManager
    },
    {
      path: '/published',
      name: 'PublishedCommunity',
      meta: { isLogin: true, grade: 'CLIENT' },
      component: PublishedCommunity
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  var user = localStorage.getItem('user')

  if (user != null) {
    user = JSON.parse(user)
  }
  if (to.name === 'UserLogin' || to.name === 'AdminLogin') {
    console.log('去登录')
    next()
    return
  }
  if (to.meta.isLogin) {
    // 判断是否需要登录
    if (token != null && user != null) {
      if (to.meta.grade === user.issuer) {
        // 判断等级
        console.log('等级够')
        next()
        return
      } else {
        router.push('/ulogin')
        return
      }
    } else {
      router.push('/ulogin')
      return
    }
  }
  next()
})

export default router
